package org.fioma.ui;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import org.fioma.tree.suffixTree.bytes.SuffixTreeBytes;

public class Fioma {

	/**
	 * @param args
	 * @throws IOException
	 * @throws NumberFormatException
	 */
	public static void main(String[] args) {
		try {
			System.out.println("Max Memory before build the tree " + Runtime.getRuntime().maxMemory());
			System.out.println("Total Memory before build the tree " + Runtime.getRuntime().totalMemory());
			System.out.println("Free Memory before build the tree " + Runtime.getRuntime().freeMemory());
			long time = System.currentTimeMillis();
			int k = Integer.parseInt(args[0]);
			SuffixTreeBytes tree = new SuffixTreeBytes(k);
			for (int i = 1; i < args.length; i++) {
				FastQ fastQ = new FastQ(args[i], k);
				tree = Tools.buildSuffixTree(fastQ, tree);
			}
			System.out.println("Max Memory after build the tree " + Runtime.getRuntime().maxMemory());
			System.out.println("Total Memory after build the tree " + Runtime.getRuntime().totalMemory());
			System.out.println("Free Memory after build the tree " + Runtime.getRuntime().freeMemory());
			System.out.println("Time to build the tree : " + (System.currentTimeMillis() - time));
			time = System.currentTimeMillis();
			BufferedWriter bw;
			if (args.length == 2) {
				bw = new BufferedWriter(new FileWriter("tree." + args[1], false));
			} else {
				bw = new BufferedWriter(new FileWriter("tree" + System.currentTimeMillis(), false));
			}
			byte[] prefix = new byte[1];
			prefix[0] = 0;
			tree.getRoot().printWords(bw, prefix);
			bw.flush();
			System.out.println("Time to save the file: " + (System.currentTimeMillis() - time));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
